package net.jltec.view.oauth;

import net.jltec.config.AppConfig;
import net.jltec.view.oauth.task.OAuthRequestTokenTask;
import net.jltec.view.oauth.task.RetrieveAccessTokenTask;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class PrepareRequestTokenActivity extends Activity {
	private OAuthConsumer consumer;
	private OAuthProvider provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		System.setProperty("debug", "true");
		consumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY,
				AppConfig.CONSUMER_SECRET);
		provider = new CommonsHttpOAuthProvider(AppConfig.REQUEST_URL,
				AppConfig.ACCESS_URL, AppConfig.AUTHORIZE_URL);

		new OAuthRequestTokenTask(this, consumer, provider).execute();
	}

	@Override
	public void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		final Uri uri = intent.getData();
		if (uri != null
				&& uri.getScheme().equals(AppConfig.OAUTH_CALLBACK_SCHEMAE)) {
			new RetrieveAccessTokenTask(this, consumer, provider, prefs)
			.execute(uri);
			
			finish();
		}
	}
}
